package com.supor.aiot.common.helper;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.widget.EditText;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: EditTextLengthHelper <br>
 * 作者: lei <br>
 * 日期: 2021/11/10 <br>
 */
public class EditTextLengthHelper {
    static EditTextLengthHelper instance;

    private EditTextLengthHelper() {

    }

    public static EditTextLengthHelper getInstance() {
        if (null == instance) {
            synchronized (EditTextLengthHelper.class) {
                if (null == instance) {
                    instance = new EditTextLengthHelper();
                }
            }
        }
        return instance;
    }

    /**
     * 设置EditText的字数限制
     *
     * @param textLength 需要限制的字符数量
     * @return
     */
    public InputFilter lengthResult(int textLength) {
        return (src, start, end, dest, dstart, dend) -> {
            int dindex = 0;
            int count = 0;

            while (count <= textLength && dindex < dest.length()) {
                char c = dest.charAt(dindex++);
                //这里是根据ACSII值进行判定的中英文，其中中文及中文符号的ACSII值都是大于128的
                if (c <= 128) {
                    count = count + 1;
                } else {
                    count = count + 2;
                }
            }

            if (count > textLength) {
                return dest.subSequence(0, dindex - 1);
            }

            int sindex = 0;
            while (count <= textLength && sindex < src.length()) {
                char c = src.charAt(sindex++);
                if (c < 128) {
                    count = count + 1;
                } else {
                    count = count + 2;
                }
            }

            if (count > textLength) {
                sindex--;
            }

            return src.subSequence(0, sindex);
        };
    }

    /**
     * EditText输入,统计字数改变
     * (中文及中文字符算两个字符,英文及英文字符算一个字符)
     *
     * @param mEditText
     * @return
     */
    public int editTextNum(EditText mEditText) {
        int mTextMaxlenght = 0;
        Editable editable = mEditText.getText();
        String str = editable.toString();
        int selEndIndex = Selection.getSelectionEnd(editable);
        for (int i = 0; i < str.length(); i++) {
            char charAt = str.charAt(i);
            if (charAt <= 128) {
                mTextMaxlenght++;
            } else {
                mTextMaxlenght += 2;
            }

            if (mTextMaxlenght > 24) {
                String newStr = str.substring(0, i);
                mEditText.setText(newStr);
                editable = mEditText.getText();
                int newLen = editable.length();
                if (selEndIndex > newLen) {
                    selEndIndex = editable.length();
                }
            }
        }
        Selection.setSelection(editable, selEndIndex);
        return mTextMaxlenght;

    }
}
